<!--
使用情况统计
-->
<template>
  <a-modal
    title="使用情况统计"
    :width="1200"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label=""
          :labelCol="{span: 24, offset: 0}"
          :wrapperCol="{span: 24, offset: 0}"
          has-feedback="">

          <div>
            图表
          </div>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {STable} from '@/components'
import {add, addList, list, page} from '@/api/modular/main/emsmaterialpool/emsMaterialPoolManage'

export default {
  components: {
    STable,
  },
  data() {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 5}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 15}
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // this.queryParam.materialType = this.type.id
        // return page(Object.assign(parameter, this.queryParam)).then((res) => {
        //   return res.data
        // })
        return new Promise((resolve, reject) => {
          let selectedRows = this.selectedRows
          let rowOne = selectedRows[0]
          let materialType = rowOne.materialType
          let poolType = rowOne.poolType
          let materialIds = selectedRows.map(value => value.materialId).join(',')
          list({materialIds, materialType, poolType}).then(res => {
            let materialPools = res.data
            selectedRows.forEach(materialPoolParam => {
              let findMaterialPoolResult = materialPools.find(value => value.materialId == materialPoolParam.materialId)
              if (findMaterialPoolResult) {
                materialPoolParam.waitNumber = findMaterialPoolResult.number
              } else {
                materialPoolParam.waitNumber = 0
              }
            })

            let value = {
              pageNo: 1,
              pageSize: -1,
              rainbow: [1],
              rows: this.selectedRows,
              totalPage: 1,
              totalRows: this.selectedRows.length
            }
            return resolve(value)
          })


        })
      },
      columns: [       // 表头
        {
          title: '序号', width:'80px',
          dataIndex: 'index',
          key: 'index',
          align: 'center',
          customRender: (text,record,index) => `${index+1}`,
        },
        // {
        //   title: '主键',
        //   dataIndex: 'materialId'
        // },
        {
          title: '料号',
          dataIndex: 'materialCode'
        },
        {
          title: '名称',
          dataIndex: 'materialName'
        },
        {
          title: '采购池待采购数量',
          dataIndex: 'waitNumber'
        },
        // {
        //   title: '库存数量',
        //   dataIndex: 'inventoryNumber'
        // },
        {
          title: '采购数量',
          dataIndex: 'inventoryWarningValue',
          width: '100px',
          scopedSlots: {customRender: 'number'}
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      selectedRows: [],
    }
  },
  methods: {
    show(record) {
      this.visible = true
      this.selectedRows.length = 0
      // {materialType: type, poolType: ORDER_TYPE_ENUM.purchaseOrder.id,selectedRows}

      for (const selectedRow of record.selectedRows) {
        this.selectedRows.push(Object.assign({
          materialPoolId: null,
          materialId: 0,
          number: 1,
          poolType: record.poolType,
          materialType: record.materialType.id,
          status: 0,
          createTime: null,
          createUser: null,
          updateTime: null,
          updateUser: null,
        }, selectedRow))
      }
      setTimeout(() => {
          this.$refs.table.refresh()
      }, 100)
    },
    handleSubmit() {
      this.confirmLoading = true
      let values = this.selectedRows.map(value => {
        let {materialPoolId, materialId, number, poolType, materialType, status, createTime, createUser, updateTime, updateUser} = value
        return {materialPoolId, materialId, number, poolType, materialType, status, createTime, createUser, updateTime, updateUser}
      })
      addList(values).then((res) => {
        this.confirmLoading = false
        if (res.success) {
          this.$message.success('新增成功')
          this.handleCancel()
          this.$emit('ok', values)
        } else {
          this.$message.info('新增失败：' + res.message)
        }
      }).finally((res) => {
        this.confirmLoading = false
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
    del(record) {
      let findIndex = this.selectedRows.findIndex(value => value == record)
      this.selectedRows.splice(findIndex, 1)
    },
  }
}
</script>
